package com.aa.utils;

import com.google.gson.Gson;

import java.lang.reflect.Type;

/**
 * Created by RIOS on 2016/6/6.
 * Copyright © 2016年. 广州虫洞网络科技有限公司. All Rights Reserved .
 */
public class GsonUtils {

    private static Gson mGson;

    public static <T> T fromJson(String json, Class<T> classOfT) {
        T object = null;
        try {
            object = getGson().fromJson(json, classOfT);
        } catch (Exception e) {
            LogUtils.d("Gson错误:"+e.getMessage());
        }
        return object;
    }

   //new TypeToken<ArrayList<HLCity>>() {}.getType()
    public static <T> T fromJson(String json, Type type) {
        T object = null;
        try {
            object = getGson().fromJson(json, type);
        } catch (Exception e) {
            LogUtils.d("Gson错误:"+e.getMessage());
        }
        return object;
    }

    public static Gson getGson() {
        if (mGson == null) {
            mGson = new Gson();
        }
        return mGson;
    }

    public static String toJson(Object o) {
        return getGson().toJson(o);
    }

}
